package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.domain.Role;
import cn.wolfcode.wms.query.EmployeeQueryObject;
import cn.wolfcode.wms.service.IDepartmentService;
import cn.wolfcode.wms.service.IEmployeeService;
import cn.wolfcode.wms.service.IRoleService;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("list")
    public String list(Map map, @ModelAttribute("qo") EmployeeQueryObject qo) {
//        int i = 1/0;
        map.put("departments", departmentService.list());
        map.put("pageResult", employeeService.query(qo));
        return "/employee/list";
    }

    @RequestMapping("input")
    public String list(Map map, Long id) {
        List<Role> roles = roleService.list();
        map.put("roles", roles);
        if (id != null) {
            Employee employee = employeeService.getById(id);
            map.put("employee", employee);
        }
        map.put("departments", departmentService.list());
        return "employee/input";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Employee emp, Long[] roleIds) {
        System.out.println(123);
        if (emp.getId() != null) {
            try {
                employeeService.update(emp, roleIds);
            } catch (Exception e) {
                return JSONResult.mark(e.getMessage());
            }
        } else {
            try {
                employeeService.save(emp, roleIds);
            } catch (Exception e) {
                return JSONResult.mark(e.getMessage());
            }
        }
        return JSONResult.success("操作成功");
    }

    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        if (id != null) {
            try {
                employeeService.delete(id);
            } catch (Exception e) {
                return JSONResult.mark("删除失败");
            }
        }
        return JSONResult.success("删除成功");
    }

    @RequestMapping("batchDelete")
    @ResponseBody
    public JSONResult delete(Long[] ids) {
        for (Long id : ids) {
            System.out.println(id);
        }
        try {
            employeeService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return JSONResult.success("批量删除成功");
    }
}
